﻿<%@page contentType="text/html; charset=UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">

<%@page import="java.util.List" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%
	String basePath = request.getContextPath();
	String pk = null;   //当前选中的物料基本分类pk
	String code = null; //当前选中的物料基本分类编码
	String name = null; //当前选中的物料基本分类名称
%>

<head>
    <title>添加产品</title>
   
    <script src="<%=basePath%>/js/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="<%=basePath%>/js/jquery.cookie.js" type="text/javascript"></script>
    <script src="<%=basePath%>/js/jquery.treeview.js" type="text/javascript"></script>        
    <link rel="stylesheet" href="<%=basePath%>/css/jquery.treeview.css" />
    <link rel="stylesheet" href="<%=basePath%>/css/ebvp.css" />
     
    <script language="javascript" type="text/javascript">    
     
     var selected_prod_cate_pk = null;
     var selected_prod_cate_code = null;
     var selected_prod_cate_name = null;      
        
        function confirmBt(){ 
        	//将 产品编码|产品名称|规格|型号 写入到父窗口中
        	var cBoxEles = document.getElementsByName("sel_cBox");   //被选中的checkbox
        	if(cBoxEles == undefined || cBoxEles.length==0){         //没有点击产品分类，直接点确定的分支
        		parent.hideMe();
        		return;
        	}else{
        		var prodListTable = parent.document.getElementById("productListTable");
        		var maxNO = (prodListTable.rows.length - 1)/2;            //添加产品分类之前，表中存在的最大序号值
        		var NO_increase = 1;                                      //序号增量
        		
        		for(var x=0; x<cBoxEles.length; x++){
        			if(cBoxEles[x].checked==false){
        				continue;
        			}
        			//序号 
        			var current_NO = Math.floor(Math.random()*10000000000000000000);//maxNO + NO_increase;
					NO_increase=NO_increase+1;
        			    
        			var valid_date_id = "valid_date" + current_NO;
        			var invalid_date_id = "invalid_date" + current_NO;
        				
        			//从子页面取数据
        			var p_name = document.getElementById("sel_prod_name"+ cBoxEles[x].value).value;
        			var moduleNameValue = document.getElementById("sel_module"+ cBoxEles[x].value).value;
        			var typeNameValue = document.getElementById("sel_type"+cBoxEles[x].value).value;
        			var pkProdVValue = document.getElementById("sel_pk_prod_v"+cBoxEles[x].value).value;
        				
        			if((maxNO + NO_increase) % 2 == 0){
        				var editRowFormStr = "<tr id='trid_"+current_NO+"'  targetid='hide_trid_"+current_NO+"' class='contTr-even' >";
        			}else{
        				var editRowFormStr = "<tr id='trid_"+current_NO+"'  targetid='hide_trid_"+current_NO+"' class='contTr-old' >";
        			}
        			editRowFormStr +="<td className='td1'><input type='checkbox' name='isCommited' id='isCommited' value=''/></td>";
        			editRowFormStr +="<td class='td1'>";
     				if(p_name != null && trimmawl(p_name) != "" && trimmawl(p_name)!="null" ){
     				    editRowFormStr += "<span class='prodName'>"+ p_name +"</span>";
     				}
     				if(moduleNameValue != null && trimmawl(moduleNameValue) != "" && trimmawl(moduleNameValue)!="null" ){
     				    editRowFormStr += "<br/><span id='hide_module'>规格:"+moduleNameValue+"</span>";
     				}
     				if(typeNameValue != null && trimmawl(typeNameValue) != "" && trimmawl(typeNameValue)!="null" ){
     				    editRowFormStr += "<br/><span id='hide_type'>型号:"+typeNameValue+"</span>";
     				}
     			    editRowFormStr +="</td>";
        			editRowFormStr += "<td class='td1'>单位:<select name='pk_unit' id='pk_unit'></select><br/>币种:<select name='pk_currency' id='pk_currency'/></td>"
        			+"<td class='td1'>含税单价:<input name='tax_unit_price' id='tax_unit_price' type='text' class='input-edit' onkeypress='javascript:keyPress(this);' onchange='subReduNum(this,"+price_digit+");javascript:doCalPrice(\"has_tax_price\",this );'/><br/>无税单价:<input name='tax_free_unit_price' id='tax_free_unit_price' type='text' class='input-edit' onkeypress='javascript:keyPress(this);' onchange='subReduNum(this,"+price_digit+");javascript:doCalPrice(\"no_tax_price\",this );'/><br/>税&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;率:<input  name='tax_rate' id='tax_rate' type='text' class='input-edit' onkeypress='javascript:keyPress(this);' onchange='subReduNum(this,2);javascript:doCalPrice(\"tax_rate\", this);'/></td>";
        			editRowFormStr += "<td class='td1'>生效日期:<input name='valid_date' id='"+valid_date_id+"'  type='text'  onclick='return showCalendar(\""+valid_date_id+"\" , \"y-mm-dd\" );' readonly='readonly' class='searchdateformwl' /><br/>失效日期:<input name='invalid_date' id='"+invalid_date_id+"' onclick='return showCalendar(\""+invalid_date_id+"\" , \"y-mm-dd\" );' type='text'  readonly='readonly' class='searchdateformwl'  /></td>";
        			editRowFormStr += "<td class='td1'>生产厂商:<select name='pk_manufactory' id='pk_manufactory'></select><br/>";
        			editRowFormStr += "运输方式:<select name='pk_transport_type' id='pk_transport_type'></select></td>";
                    editRowFormStr += "<td class='td1'><label class='linkstyle3' onclick='savePriceDetail(this)' >保存</label>";
        			editRowFormStr += "<input name='primaryKey' id='primaryKey' type='hidden' />";
        			editRowFormStr += "<input name='prod_cate_pk' id='prod_cate_pk' type='hidden' value='"+ selected_prod_cate_pk +"' />";
					editRowFormStr += "<input name='pk_prod_v' id='pk_prod_v' type='hidden' value='"+pkProdVValue+"' /> </td>";
					editRowFormStr += "</tr>";
        				
        				//区分
        				if((maxNO + NO_increase) % 2 == 0){
        					var hideRowStr = "<tr id='hide_trid_"+current_NO+"' targetid='trid_"+current_NO+"'  class='contTr-even' style='display: none;'>";
        				}else{
        					var hideRowStr = "<tr id='hide_trid_"+current_NO+"'  targetid='trid_"+current_NO+"' class='contTr-old' style='display: none;'>";
        				}
        				hideRowStr +="<td class='td1'><input type='checkbox' name='isCommited' id='isCommited' value='' /></td>";
        				hideRowStr +="<td class='td1'>";
     				    if(p_name != null && trimmawl(p_name) != "" && trimmawl(p_name)!="null" ){
     				    	hideRowStr += "<span class='prodName'>"+ p_name +"</span>";
     				    }
     				    if(moduleNameValue != null && trimmawl(moduleNameValue) != "" && trimmawl(moduleNameValue)!="null" ){
     				    	hideRowStr += "<br/><span id='hide_module'>规格:"+moduleNameValue+"</span>";
     				    }
     				    if(typeNameValue != null && trimmawl(typeNameValue) != "" && trimmawl(typeNameValue)!="null" ){
     				    	hideRowStr += "<br/><span id='hide_type'>型号:"+typeNameValue+"</span>";
     				    }
     				    hideRowStr +="</td>";
     				    hideRowStr +="<td class='td1'><span id='hide_unit'>单位:</span><br/><span id='hide_currency_name'>币种:</span></td>";
        				hideRowStr +="<td class='td1'>含税单价:<span id='hide_tax_unit_price'></span><br/>无税单价:<span id='hide_tax_free_unit_price' class='input-unEdit'></span><br/>税&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;率:<span id='hide_tax_rate' class='input-unEdit'</span></td>";
        				hideRowStr += "<td class='td1'>生效日期:<span id='hide_valid_date'></span><br/>失效日期:<span id='hide_invalid_date'></span></td>";
        				hideRowStr +="<td class='td1'><span id='hide_manufactory_name'>生产厂商:</span><br/>";
        				hideRowStr +="<span id='hide_transport_type'>运输方式:</span></td>";
        				hideRowStr +="<td class='td1'><label class='linkstyle3' onclick='modifyPriceDetail(this)'>修改</label></td>";
        				hideRowStr +="</tr>";
        				
        				var pTable = parent.document.getElementById("productListTable");
        				$('#productListTable', window.parent.document).append(editRowFormStr);
        				$('#productListTable', window.parent.document).append(hideRowStr);
        				
        				var t = "#trid_" + current_NO;
        				$(t, window.parent.document).wrap("<form id='form_"+current_NO+"'></form>");  
        				
        			   
        			   //查询税率
        			   doQueryTaxRate($(t, window.parent.document));
        			    
        			   //初始化 单位和生产厂商
        			   initUnitAndManufacure($(t, window.parent.document));
        			    
        			   //初始化 币种
        			   initCurrency($(t, window.parent.document));
        			    
        			   //初始化 运输方式
        			   initTransport($(t, window.parent.document));
        			    
        		}        		
        	} 
        	       	    	
			parent.hideMe();
        }
        
        
        function cancelBt(){
        	parent.hideMe();
        }
          
                 
        function showProd(prodCatePk, prodCateCode, prodCateName){
                	
        	var myts = new Date();    	
            jQuery.ajax
            (
                	{	
                		type: "GET", 
                    	async:false, 
                   		url:"<%=basePath%>/prodprice/qryProdByProdcatePk?prodCatePk=" + prodCatePk + "&myts="+myts, 
                   		dataType:"text", 
                    	success:function(result)
                    	{ 
	                        if(result =='NULL')
	                        { 
	                             table = document.getElementById("prodTable");
	                             for(var w=table.rows.length-1;  w>=0; w--){
		    						table.deleteRow(w);
		    					 }	
	                        } else{
	                        	table = document.getElementById("prodTable");
	                        	for(var w=table.rows.length-1;  w>=0; w--){
		    						table.deleteRow(w);
		    					}
                			    
                			    selected_prod_cate_pk = prodCatePk;
                			    selected_prod_cate_code = prodCateCode;
                			    selected_prod_cate_name = prodCateName;
                			    					
		    					for(var j = 0; j < eval(result).length; j++){
		    						var obj = eval(result)[j];
		    						
		    						row = table.insertRow(-1);
		    						if(j % 2 == 1){
		    							row.className="contTr-old";
		    						}else{
		    							row.className="contTr-even";
		    						}
		    						cell1 = row.insertCell();
		    						cell1.setAttribute("width", "20");
		    						cell1.className="td1";
		    						var cccBox = document.createElement("<input id='sel_cBox'  type='checkbox' name='sel_cBox' value='"+j+"'/>");
		    						cccBox.setAttribute("checked", false);
		    						//需求:已经存在的产品应不允许重复添加
		    						//实现:查询父窗口中是否已有该产品，如果有则check box不可用
		    						if(isProdExist(obj.pk_prod_v)){
		    							//cccBox.setAttribute("disabled", "true");
		    							continue;
		    						}else{
		    							cell1.appendChild(cccBox);
		    						}
		    						
		    						cell2 = row.insertCell();
		    						cell2.className="td1";
		    						cell2.innerText = obj.prod_name;	     //把内容设为产品名称，带到父窗口中
		    						//在cell2里冗余存一下 主键\名称\规格\型号
		    						elePkProd = document.createElement("<input type='hidden' id='sel_pk_prod_v"+j+"' value='"+obj.pk_prod_v+"'/>");
		    						eleProdName = document.createElement("<input type='hidden' id='sel_prod_name"+j+"' value='"+obj.prod_name+"'/>");
		    						eleModule = document.createElement("<input type='hidden' id='sel_module"+j+"' value='"+obj.prod_module+"'/>");
		    						eleType = document.createElement("<input type='hidden' id='sel_type"+j+"' value='"+obj.prod_type+"'/>");
		    						cell2.appendChild(elePkProd);
		    						cell2.appendChild(eleProdName);
		    						cell2.appendChild(eleModule);
		    						cell2.appendChild(eleType);
		    					}
		    					if(document.getElementsByName("sel_cBox")==null || document.getElementsByName("sel_cBox").length==0){
		    						alert("该产品分类下已无可增加的产品!");
		    					}
	                        }
                    	} 
               	 	} 
            ) 
        }
        
        //判断指定产品是否已添加
        function isProdExist(pkProdv){
        	if(pkProdv == null){
        		return false;
        	}
        	var pkProdvEleArray = parent.document.getElementsByName("pk_prod_v");
        	if(pkProdvEleArray==null || pkProdvEleArray.length==0){
        		return false;
        	}
        	
        	var prodListTable = parent.document.getElementById("productListTable");
        	for(var x=0; x<pkProdvEleArray.length; x++){
        		var pkProdvEle = pkProdvEleArray[x];
        		if(pkProdvEle!=null && (pkProdvEle.value==pkProdv)){
        			if(prodListTable.rows(x+1).style.display=="none"){  //该行已经被删
        				;
        			}else{
	        			return true;
        			}
        		}
        	}
        	//以上逻辑只能判断当前页是否已添加了该产品
        	//下面逻辑判断，所有页面是否包含该产品
        	var bGlobal = false;
        	var myts = new Date();    	
            jQuery.ajax
            (
                	{	
                		type: "GET", 
                    	async:false, 
                   		url:"<%=basePath%>/prodprice/isPkProdVExist?pk_prod_v=" + pkProdv + "&myts="+myts, 
                   		dataType:"text", 
                    	success:function(result)
                    	{                     		
	                        if(result =='true')
	                        { 
	                            bGlobal = true;    //这里的return指的是从success:function里return，并不是从isProdExist方法中return
	                        } else{
	                        	bGlobal = false;   //这里的return指的是从success:function里return，并不是从isProdExist方法中return
	                        }
	                    }
	                 }
	           )
        	
        	return bGlobal;
        }
        
        
        //初始化单位
    	function initUnitAndManufacure(trNode){
    		//从父窗口取pk_prod_v
    		var pk_prod_v_value = "";
			var pk_prod_v_ele = trNode.find("#pk_prod_v");
			if(pk_prod_v_ele != null){
				pk_prod_v_value = pk_prod_v_ele.val();
			}
    		var myts = new Date();
            jQuery.ajax
            (
                	{	
                		type: "GET", 
                    	async:false, 
                   		url:"<%=basePath%>/prodprice/queryUnitByPkProdV?pk_prod_v="+ pk_prod_v_value +"&myts="+myts, 
                   		dataType:"text", 
                    	success:function(result)
                    	{ 
                        	var ele_pk_unit= trNode.find("#pk_unit");
                        	if(result == 'NULL'){
                    			ele_pk_unit.empty();
                    		}else{
                    			ele_pk_unit.empty();
                    			var unitArray = eval("("+result+")");
                    			for(var index=0; index<unitArray.length; index++){
                    				var obj = unitArray[index];
							        ele_pk_unit.append("<option value='"+obj.pk_unit+"'>"+obj.name+"</option>");
                    			}
                    		}                    	
	                    } 
               	 	} 
            ) 
            
            jQuery.ajax
            (
                	{	
                		type: "GET", 
                    	async:false, 
                   		url:"<%=basePath%>/prodprice/queryManufactureByPkProdV?pk_prod_v="+ pk_prod_v_value +"&myts="+myts, 
                   		dataType:"text", 
                    	success:function(result1)
                    	{ 
                    		var ele_pk_manufactory= trNode.find("#pk_manufactory");
                    		if(result1 == 'NULL'){
                    			 ele_pk_manufactory.empty();
                    		}else{
                    			ele_pk_manufactory.empty();
                    			var manufactArray = eval("("+result1+")");
                    			for(var index=0; index<manufactArray.length; index++){
                    				var obj = manufactArray[index];
							        ele_pk_manufactory.append("<option value='"+obj.pk_manufacture+"'>"+obj.name+"</option>");
                    			}
                    		}                    	
	                    } 
              	 	} 
            ) 
              		
    	}
    	
    	function doQueryTaxRate(trNode){
			var pk_prod_v_ele  = trNode.find("#pk_prod_v");
			if(pk_prod_v_ele != null){
				pk_prod_v_val = pk_prod_v_ele.val();
			}
			var myts = new Date();
            jQuery.ajax
            (
                {	
                	type: "GET", 
                    async:false, 
                   	url:"<%=basePath%>/prodprice/queryTaxRateByPkProdV?pk_prod_v="+ pk_prod_v_val +"&myts="+myts, 
                   	dataType:"text", 
                    success:function(result)
                    { 
                    	trNode.find("#tax_rate").val(result);                   			                 	
	                } 
               	 } 
            ) 
		}
		
		function initCurrency(trNode){
			var myts = new Date();
            jQuery.ajax
            (
                {	
                	type: "GET", 
                    async:false, 
                   	url:"<%=basePath%>/prodprice/queryCurrency?myts="+myts, 
                   	dataType:"text", 
                    success:function(result)
                    {
                    	var ele_pk_currency = trNode.find("#pk_currency");
                    	if(result == 'NULL'){
                    		ele_pk_currency.empty();
                        }else{
                        	ele_pk_currency.empty();
                    		var cnyArray = eval("("+result+")");
                    		for(var index=0; index<cnyArray.length; index++){
                    				var obj = cnyArray[index];
							        ele_pk_currency.append("<option value='"+obj.pk_currtype+"'>"+obj.name+"</option>");
                    		}
                    	}                			                 	
	                } 
               	 } 
            ) 
		}
        
        function initTransport(trNode){
			var myts = new Date();
            jQuery.ajax
            (
                {	
                	type: "GET", 
                    async:false, 
                   	url:"<%=basePath%>/prodprice/queryTransport?myts="+myts, 
                   	dataType:"text", 
                    success:function(result)
                    {
                    	var ele_pk_transport = trNode.find("#pk_transport_type");
                    	if(result == 'NULL'){
                    		ele_pk_transport.empty();
                    	}else{
                    		ele_pk_transport.empty();
                    		var transportArray = eval("("+result+")");
                    		for(var index=0; index<transportArray.length; index++){
                    				var obj = transportArray[index];
							        ele_pk_transport.append("<option value='"+obj.pk_transporttype+"'>"+obj.name+"</option>");
                    		}
                    	}                			                 	
	                } 
               	 } 
            ) 
		}
		
    </script>
</head>
<body class="innerpage" >
    <div class="margin10">
    </div>
    <div class="product">
        <div class="view">
            <table cellpadding="0" cellspacing="0" border="0" class="tableCss">
						<%
							String strCount = request.getAttribute("PROD_CATE_COUNT").toString();
							int iCount = Integer.parseInt(strCount);
							
							List prodcatePkList   = (List)request.getAttribute("PROD_CATE_PK_LIST");
							List prodcateCodeList = (List)request.getAttribute("PROD_CATE_CODE_LIST");
							List prodcateNameList = (List)request.getAttribute("PROD_CATE_NAME_LIST");
							for(int i=0; i<iCount; i++)
							{
								pk   = prodcatePkList.get(i).toString();
								code = prodcateCodeList.get(i).toString();
								name = prodcateNameList.get(i).toString();
						%>
							<tr>
								<td>
									<label class="linkstyle1" onclick="showProd('<%=pk%>', '<%=code%>', '<%=name%>')"> <%out.print(name);%></label>
								</td>
							</tr>						  				
						<%			
							}
						%>
			</table>
        </div>
        
        <div class="view">
            <table border="0" id="prodTable" class="tableCss">
                <tr>
                </tr>
            </table>           
        </div>
    </div>
	    <div class="margin10">
	    </div>
    	
       <div align="right">
			<input type="button" class="buttonok" value="确定"  onclick="confirmBt()"/>
					&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="button" class="buttoncan" value="取消" onclick="cancelBt()"/>
	   </div>
  </body>
</html>
